home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: convert 4 8-bit bytes in a foat
- Date: Mon, 08 Apr 96 12:21:00 GMT
- Organization: none
- Message-ID: <828966060snz@genesis.demon.co.uk>
- References: <4k5pr7$bp7@nuscc.nus.sg>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4k5pr7$bp7@nuscc.nus.sg>
- eng30183@leonis.nus.sg "LEONG TUCK WAI" writes:
-
- >Hi,
- >
- >I have 4 8-bit bytes. If I line them up in order, they actually represent
- >the 32-bit float data type version of a decimal point number. So, how do
- >I get the compiler to understand that, and print out the float number
- >instead of giving me 4 integers?
-
- float f;
- unsigned char a[sizeof f];
-
- /* Write your bytes to a by whatever means */
-
- memcpy(&f, a, sizeof f);
-
- printf("%f\n", f);
-
- Another possibility is:
-
- float f;
- unsigned char *ptr = (unsigned char *)&f;
-
- You can then use ptr to write the bytes into f directly. Using unions, or
- char instead of unsigned char will typically work but is more likely to
- trip up on the letter of the standard. If you are reading from a binary
- file you could write:
-
- fread(&f, sizeof f, 1, fp)
-
- That does assume that byte order and so on in the file are correct since
- you don't get a chance to alter them prior to loading into f.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-